function $(a) {
  return document.querySelector(a);
}

window.onload = function () {
  var tbody = document.querySelector("tbody");
  // 默认头像
  var imgsrc = "/publics/back-stage/img/R-C (1).jpg";

  // 退出登录
  // $('.exit').onclick=function(){
  //     axios({
  //         method: 'post',
  //         url: '/user/logout',
  //     }).then(function(data){
  //        alert('退出登录')
  //     }).catch(err=>{
  //         console.log(err);
  //     })
  // }

  //  全部数据
  $(".alllist").onclick = function () {
    $(".pageSize").style.display = "block";
    $(".pageSize1").style.display = "none";
    $(".nowpage-1").innerHTML = 1;
    $(".nowpage").value = $(".nowpage-1").innerHTML;
    $("#search").value = "";
    fenye(1, $(".pageSize").value);
  };
  // 点击显示数据每页几条
  $(".pageSize").onchange = function () {
    $(".nowpage-1").innerHTML = 1;
    $(".nowpage").value = $(".nowpage-1").innerHTML;
    fenye(1, $(".pageSize").value);
  };
  $(".pageSize1").onchange = function () {
    $(".nowpage-1").innerHTML = 1;
    $(".nowpage").value = $(".nowpage-1").innerHTML;
    searchdata(1, $(".pageSize1").value);
  };

  // 弹窗显示
  function popup(a, b, c) {
    a.addEventListener("click", function () {
      b.style.display = "block";
    });
    c.addEventListener("click", function () {
      b.style.display = "none";
    });
  }
  // popup($(".add-all"),$(".add-box"),$(".back"));

  // // 分页数据
  function list(data, b) {
    let list = data.data.data.object.list;
    let pageNow = data.data.data.object.pageNow;
    for (let i = 0; i < list.length; i++) {
      let tds = ` <tr>
        <td><input name="checkbox" type="checkbox" value="checkbox" class="chexkbox" /></td>
        <td>${i + 1 + (pageNow - 1) * $(".pageSize").value}</td>
        <td>${list[i].videoName == null ? "无" : list[i].videoName}</td>
        <td><img src="${
          list[i].src == "未上传" ? "#" : list[i].src
        }" style="width:40px;height:40px;border-radius: 50%;"></img></td>
        <td>${list[i].nickname == null ? "无" : list[i].nickname}</td>
        <td>${list[i].content == null ? "无" : list[i].content}</td>
        <td>${list[i].releaseTime == null ? "无" : list[i].releaseTime}</td>
        <td>${
          list[i].thumbsUpComment == null ? "无" : list[i].thumbsUpComment
        }</td>
        <td><button class='commentSon-btn'>子评论</button><button class='delete-user'><i class="fa fa-trash-o" aria-hidden="true"></i> 删除</button></td>
    </tr>`;
      $("tbody").insertAdjacentHTML("beforeend", tds);
      var tbr = tbody.querySelectorAll("tr");
      if (i % 2 == 0) {
        tbr[i].style.backgroundColor = "rgb(220, 220, 220)";
        tbr[i].onmouseover = function () {
          this.style.backgroundColor = "rgb(202, 170, 145)";
        };
        tbr[i].onmouseout = function () {
          this.style.backgroundColor = "rgb(220, 220, 220)";
        };
      }
    }
    // 子评论按钮
    var commentSon_btn = document.querySelectorAll(".commentSon-btn");
    for (let i = 0; i < commentSon_btn.length; i++) {
      popup(commentSon_btn[i], $(".commentson-box"), $(".back"));
      commentSon_btn[i].onclick = function () {
        var commentId = list[i].commentId;
        console.log(commentId);
        commentSon(commentId);
      };
    }
    // 删除按钮绑定
    var del_btn = document.querySelectorAll(".delete-user");
    for (let i = 0; i < del_btn.length; i++) {
      del_btn[i].onclick = function () {
        var x = 2;
        if ((x = 2)) {
          console.log(x);
          $(".form-pop").querySelector("p").innerHTML = "你确定要删除吗？";
          $(".pop-box").style.display = "block";
          $(".submit-pop1").onclick = function () {
            var commentId = list[i].commentId;
            delete_comment(commentId, b);
            $(".pop-box").style.display = "none";
          };
          $(".submit-pop2").onclick = function () {
            $(".pop-box").style.display = "none";
          };
          $(".back-pop").onclick = function () {
            $(".pop-box").style.display = "none";
          };
        }
      };
    }

    //  分页的总量
    var pageTotal = data.data.data.object.pageTotal;
    var pageTotalCount = data.data.data.object.pageTotalCount;

    // 跳转按钮
    $(".nowpage").value = $(".nowpage-1").innerHTML;
    $(".nowpage-btn-2").onclick = function () {
      $(".nowpage-1").innerHTML =
        $(".nowpage").value > pageTotal ? pageTotal : $(".nowpage").value;
      searchdata($(".nowpage-1").innerHTML, $(".pageSize").value);
    };
    $(".nowpage-btn-1").onclick = function () {
      $(".nowpage-1").innerHTML =
        $(".nowpage").value > pageTotal ? pageTotal : $(".nowpage").value;
      fenye($(".nowpage-1").innerHTML, $(".pageSize").value);
    };
    $(".total").innerHTML = `共${pageTotal}页共${pageTotalCount}条数据`;
    if (pageTotal < 3) {
      for (let i = 0; i < pageTotal; i++) {
        let a = ` <a href="JavaScript:;" class='pagebtn'>${i + 1}</a>`;
        $(".pageye").insertAdjacentHTML("beforeend", a);
      }
    } else {
      for (let i = 0; i < 3; i++) {
        let a = ` <a href="JavaScript:;" class='pagebtn'>${i + 1}</a>`;
        $(".pageye").insertAdjacentHTML("beforeend", a);
      }
      var pagebtnAll = document.querySelectorAll(".pagebtn");
      if ($(".nowpage").value >= 3) {
        let c = $(".nowpage").value;
        let a = c >= pageTotal - 1 ? (c = pageTotal - 1) : c;
        pagebtnAll[0].innerHTML = a - 1;
        pagebtnAll[1].innerHTML = a;
        pagebtnAll[2].innerHTML = ++a;
      }
    }
    var pagebtnAll = document.querySelectorAll(".pagebtn");
    for (let i = 0; i < pagebtnAll.length; i++) {
      pagebtnAll[i].onclick = function () {
        b(this.innerHTML, $(".pageSize").value);
        $(".nowpage-1").innerHTML = `${this.innerHTML}`;
        $(".nowpage").value = $(".nowpage-1").innerHTML;
      };
      if (pagebtnAll[i].innerHTML == $(".nowpage-1").innerHTML) {
        pagebtnAll[i].style.backgroundColor = "rgb(202,170,145)";
      } else if (pagebtnAll[i].innerHTML == $(".nowpage").value) {
        for (let j = 0; j < pagebtnAll.length; j++) {
          pagebtnAll[j].style.backgroundColor = "";
        }
        pagebtnAll[i].style.backgroundColor = "rgb(202,170,145)";
      }
    }
    $("#next").onclick = function () {
      var x = $(".nowpage-1").innerHTML;
      x++;
      var y = x >= pageTotal ? (x = pageTotal) : x;
      $(".nowpage-1").innerHTML = `${y}`;
      $(".nowpage").value = $(".nowpage-1").innerHTML;
      b($(".nowpage-1").innerHTML, $(".pageSize").value);
      console.log($(".nowpage-1").innerHTML, $(".nowpage").value);
      console.log($(".nowpage").value);
    };
    $("#pre").onclick = function () {
      var x = $(".nowpage-1").innerHTML;
      x--;
      var y = x <= 1 ? (x = 1) : x;
      $(".nowpage-1").innerHTML = `${y}`;
      $(".nowpage").value = $(".nowpage-1").innerHTML;
      b($(".nowpage-1").innerHTML, $(".pageSize").value);
    };
    $(".frist").onclick = function () {
      $(".nowpage-1").innerHTML = `1`;
      $(".nowpage").value = $(".nowpage-1").innerHTML;
      b(1, $(".pageSize").value);
    };
    $(".last").onclick = function () {
      $(".nowpage-1").innerHTML = `${pageTotal}`;
      $(".nowpage").value = $(".nowpage-1").innerHTML;
      b(pageTotal, $(".pageSize").value);
    };
    // 复选框
    var checkbox = document.querySelectorAll(".chexkbox");
    $("#checkbox-all").onclick = function () {
      if ($("#checkbox-all").checked) {
        for (let i = 0; i < checkbox.length; i++) {
          checkbox[i].checked = "checked";
        }
      } else {
        for (let i = 0; i < checkbox.length; i++) {
          checkbox[i].checked = "";
        }
      }
    };
    // $('.delete-all').onclick=function(){
    //     console.log(1);
    //     if(confirm('你是否要批量删除')){
    //     for(let i = 0 ;i< checkbox.length; i++){
    //         if(checkbox[i].checked){
    //             console.log(list[i].commentId);
    //         delete_Allcomment(list[i].commentId);
    //         }
    // }
    // alert('删除成功')
    // fenye($('.nowpage-1').innerHTML,$('.pageSize').value);
    // }
    // }

    $(".delete-all").onclick = function () {
      var x = 3;
      if ((x = 3)) {
        console.log(x);
        $(".form-pop").querySelector("p").innerHTML = "你确定要批量删除吗？";
        $(".pop-box").style.display = "block";
        $(".submit-pop1").onclick = function () {
          for (let i = 0; i < checkbox.length; i++) {
            if (checkbox[i].checked) {
              console.log(list[i].commentId);
              delete_Allcomment(list[i].commentId);
            }
          }
          alert("删除成功");
          b($(".nowpage-1").innerHTML, $(".pageSize").value);
          $(".pop-box").style.display = "none";
        };
        $(".submit-pop2").onclick = function () {
          $(".pop-box").style.display = "none";
        };
        $(".back-pop").onclick = function () {
          $(".pop-box").style.display = "none";
        };
      }
    };
  }

  // 渲染数据
  function fenye(a, b) {
    axios({
      method: "get",
      url: "/findAllComment",
      params: {
        pageNow: a,
        pageSize: b,
      },
    })
      .then(function (data) {
        $("tbody").innerHTML = "";
        $(".pageye").innerHTML = "";
        list(data, fenye);
        $("#checkbox-all").checked = "";
      })
      .catch((err) => {
        console.log(err);
      });
  }
  fenye(1, 10);
  // 模糊查询
  function searchdata(a, b) {
    var search_input = document.querySelector("#search");
    axios({
      method: "get",
      url: "/findCommentByContentLike",
      params: {
        content: search_input.value,
        pageNow: a,
        pageSize: b,
      },
    })
      .then(function (data) {
        if (data.data.data.object.list.length == 0) {
          $("#search").value = "";
          $(".pageSize1").style.display = "none";
          $(".pageSize").style.display = "";
          $(".nowpage-btn-2").style.display = "none";
          $(".nowpage-btn-1").style.display = "";
          $(
            ".not"
          ).innerHTML = `<i class="fa fa-exclamation-circle" style="color: rgb(255, 187, 0);" aria-hidden="true"></i>抱歉找不到`;
          $(".not").style.display = "";
          setTimeout(() => {
            $(".not").style.display = "none";
          }, 1500);
        } else {
          $("tbody").innerHTML = "";
          $(".pageye").innerHTML = "";
          $(".pageSize1").style.display = "";
          $(".pageSize").style.display = "none";
          $(".nowpage-btn-2").style.display = "";
          $(".nowpage-btn-1").style.display = "none";
          list(data, searchdata);
        }
      })
      .catch((err) => {
        console.log(err);
      });
  }
  //  点击搜索按钮
  $(".search-btn").addEventListener("click", function () {
    $(".nowpage-1").innerHTML = 1;
    $(".nowpage").value = $(".nowpage-1").innerHTML;
    var search_input = document.querySelector("#search");
    if (search_input.value == "") {
      $(
        ".not"
      ).innerHTML = `<i class="fa fa-exclamation-circle" style="color: rgb(255, 187, 0);" aria-hidden="true"></i>抱歉找不到`;
      $(".not").style.display = "";
      setTimeout(() => {
        $(".not").style.display = "none";
      }, 1500);
    } else {
      $(
        ".not"
      ).innerHTML = `<i class="fa fa-check" aria-hidden="true" style="color:green"></i>搜索成功`;
      $(".not").style.display = "";
      setTimeout(() => {
        $(".not").style.display = "none";
      }, 1500);
      searchdata(1, $(".pageSize").value);
    }
  });

  // 删除单个数据
  function delete_comment(commentId, b) {
    axios({
      method: "delete",
      url: "/deleteCommentByCommentId",
      params: {
        commentId: commentId,
      },
    })
      .then(function (data) {
        $(
          ".not"
        ).innerHTML = `<i class="fa fa-check" aria-hidden="true" style="color:green"></i>删除成功`;
        $(".not").style.display = "";
        setTimeout(() => {
          $(".not").style.display = "none";
        }, 1500);
        b($(".nowpage-1").innerHTML, $(".pageSize").value);
      })
      .catch((err) => {
        console.log(err);
      });
  }
  // 批量删除
  function delete_Allcomment(commentId) {
    axios({
      method: "delete",
      url: "/deleteCommentList",
      params: {
        commentId: commentId,
      },
    })
      .then(function (data) {
        $(
          ".not"
        ).innerHTML = `<i class="fa fa-check" aria-hidden="true" style="color:green"></i>删除成功`;
        $(".not").style.display = "";
        setTimeout(() => {
          $(".not").style.display = "none";
        }, 1500);
      })
      .catch((err) => {
        console.log(err);
      });
  }
  // 遍历子评论
  function commentSon(commentId) {
    axios({
      method: "get",
      url: "/findCommentSonByCommentId",
      params: {
        commentId: commentId,
      },
    })
      .then(function (data) {
        $(".listbody").innerHTML = "";
        listcommentSon(data);
      })
      .catch((err) => {
        console.log(err);
      });
  }
  // 批量子评论删除
  function delete_AllcommentSon(commentSonId) {
    axios({
      method: "delete",
      url: "/deleteCommentSonList",
      params: {
        commentSonId: commentSonId,
      },
    })
      .then(function (data) {
        $(
          ".not"
        ).innerHTML = `<i class="fa fa-check" aria-hidden="true" style="color:green"></i>删除成功`;
        $(".not").style.display = "";
        setTimeout(() => {
          $(".not").style.display = "none";
        }, 1500);
      })
      .catch((err) => {
        console.log(err);
      });
  }

  // 单个子评论删除
  function delete_commentSon(a) {
    axios({
      method: "delete",
      url: "/deleteCommentSonByCommentSonId",
      params: {
        commentSonId: a,
      },
    })
      .then(function (data) {
        let da = data.data.data.object;
        $(
          ".not"
        ).innerHTML = `<i class="fa fa-check" aria-hidden="true" style="color:green"></i>删除成功`;
        $(".not").style.display = "";
        setTimeout(() => {
          $(".not").style.display = "none";
        }, 1500);
        commentSon(da.commentId);
      })
      .catch((err) => {
        console.log(err);
      });
  }
  // 子评论数据
  function listcommentSon(data) {
    let list = data.data.data.object;
    for (let i = 0; i < list.length; i++) {
      let tds = ` <tr>
        <td><input name="checkbox" type="checkbox" value="checkbox" class="chexkbox1" /></td>
        <td>${i + 1}</td>
        <td><img src="${
          list[i].srcOne == "未上传" ? "#" : list[i].srcOne
        }" style="width:40px;height:40px;border-radius: 50%;"></img></td>
        <td>${list[i].nicknameOne == null ? "无" : list[i].nicknameOne}</td>
        <td><img src="${
          list[i].srcTwo == "未上传" ? "#" : list[i].srcTwo
        }" style="width:40px;height:40px;border-radius: 50%;"></img></td>
        <td>${list[i].nicknameTwo == null ? "无" : list[i].nicknameTwo}</td>
        <td>${list[i].content == null ? "无" : list[i].content}</td>
        <td>${list[i].releaseTime == null ? "无" : list[i].releaseTime}</td>
        <td><button class='delete-commentSon'><i class="fa fa-trash-o" aria-hidden="true"></i> 删除</button></td>
    </tr>`;
      $(".listbody").insertAdjacentHTML("beforeend", tds);
      let tbr = $(".listbody").querySelectorAll("tr");
      if (i % 2 == 0) {
        tbr[i].style.backgroundColor = "rgb(220, 220, 220)";
        tbr[i].onmouseover = function () {
          this.style.backgroundColor = "rgb(202, 170, 145)";
        };
        tbr[i].onmouseout = function () {
          this.style.backgroundColor = "rgb(220, 220, 220)";
        };
      }
    }
    // 删除按钮绑定
    let delete_commentSonbtn = document.querySelectorAll(".delete-commentSon");
    for (let i = 0; i < delete_commentSonbtn.length; i++) {
      delete_commentSonbtn[i].onclick = function () {
        var x = 3;
        if ((x = 3)) {
          console.log(x);
          $(".form-pop1").querySelector("p").innerHTML = "你确定要删除吗？";
          $(".pop1-box").style.display = "block";
          $(".submit1-pop1").onclick = function () {
            let commentSonId = list[i].commentSonId;
            console.log(commentSonId);
            delete_commentSon(commentSonId);
            $(".pop1-box").style.display = "none";
          };
          $(".submit1-pop2").onclick = function () {
            $(".pop1-box").style.display = "none";
          };
          $(".back-pop1").onclick = function () {
            $(".pop1-box").style.display = "none";
          };
        }
      };
    }
    // // // 删除按钮
    // let delete_commentSonbtn = document.querySelectorAll('.delete-commentSon')
    // for(let i = 0 ; i<delete_commentSonbtn.length;i++){
    //     delete_commentSonbtn[i].onclick=function(){
    //         let commentSonId=list[i].commentSonId
    //         console.log(commentSonId);
    //         delete_commentSon(commentSonId);
    // //     }
    // // }
    // 复选框
    var checkbox1 = document.querySelectorAll(".chexkbox1");
    $("#checkbox-all1").onclick = function () {
      if ($("#checkbox-all1").checked) {
        for (let i = 0; i < checkbox1.length; i++) {
          checkbox1[i].checked = "checked";
        }
      } else {
        for (let i = 0; i < checkbox1.length; i++) {
          checkbox1[i].checked = "";
        }
      }
    };
    $(".delete-commentSonAll").onclick = function () {
      if (confirm("你是否要批量删除")) {
        for (let i = 0; i < checkbox1.length; i++) {
          if (checkbox1[i].checked) {
            let commentSonId = list[i].commentSonId;
            console.log(commentSonId);
            delete_AllcommentSon(commentSonId);
          }
        }
        $(
          ".not1"
        ).innerHTML = `<i class="fa fa-times-circle-o" aria-hidden="true"  style="color:red;"></i>删除成功`;
        $(".not1").style.display = "";
        setTimeout(() => {
          $(".not1").style.display = "none";
        }, 2000);
        commentSon(list[0].commentId);
      }
    };

    $(".delete-commentSonAll").onclick = function () {
      var x = 3;
      if ((x = 3)) {
        console.log(x);
        $(".form-pop1").querySelector("p").innerHTML = "你确定要批量删除吗？";
        $(".pop1-box").style.display = "block";
        $(".submit1-pop1").onclick = function () {
          for (let i = 0; i < checkbox1.length; i++) {
            if (checkbox1[i].checked) {
              let commentSonId = list[i].commentSonId;
              console.log(commentSonId);
              delete_AllcommentSon(commentSonId);
            }
          }
          alert("删除成功");
          commentSon(list[0].commentId);
          $(".pop1-box").style.display = "none";
          $(".submit1-pop2").onclick = function () {
            $(".pop1-box").style.display = "none";
          };
          $(".back-pop1").onclick = function () {
            $(".pop1-box").style.display = "none";
          };
        };
      }
    };
  }
};
